package com.sunreal.paygateway.sys.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 * Description: Md5Util
 * Author: liuzh
 * Update: liuzh(2014-04-04 09:37)
 */
public class Md5Util {
    private static final Logger logger = LoggerFactory.getLogger(Md5Util.class);

    private static final MD5 MD_5 = new MD5();

    /**
     * 转换为Md5
     *
     * @param md5s
     * @return
     * @throws Exception
     */
    public static String id(String... md5s) throws Exception {
        try {
            return toMd5(md5s);
        } catch (Exception e) {
            throw new Exception("生成主键失败!");
        }
    }

    /**
     * 转换为Md5
     *
     * @param md5s
     * @return
     * @throws Exception
     */
    public static String toMd5(String... md5s) throws Exception {
        if (md5s == null || md5s.length == 0) {
            throw new Exception("至少需要一个参数!");
        }
        return MD_5.getMD5ofStr(md5s);
    }


    /**
     * 以自身为盐的加密
     *
     * @param value
     * @return
     * @throws Exception
     */
    public static String toMd5WithItSelfAsSalt(String value) {
        try {
            return toMd5WithSalt(value, value);
        } catch (Exception e) {
            logger.info("加密失败,原文:" + value + ",错误信息:" + e.getMessage());
            return null;
        }
    }

    /**
     * 带盐的加密
     *
     * @param value
     * @param salt
     * @return
     * @throws Exception
     */
    public static String toMd5WithSalt(String value, String salt) throws Exception {
        if (value == null || salt == null || value.equals("") || salt.equals("")) {
            throw new Exception("参数不能为空");
        }
        return MD_5.getMD5ofStr(MD_5.getMD5ofStr(value) + salt);
    }


}
